/ leetcodesolve / 125. Valid Palindrome.py
swadhinbiswas solve more problem · 5/11/2025
8bcf412
28 lines · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
    def isAlpha(self,ch)->bool:
      if (ch>='0' and ch<='9') or (ch.lower()>='a' and ch.lower()<='z') :
        return True
    def isPalindrome(self, s: str) -> bool:
      start,end=0,len(s)-1
      while(start<end):
        if not self.isAlpha(s[start]):
          start+=1
          continue
        if not self.isAlpha(s[end]):
          end-=1
          continue
        if s[start].lower()!=s[end].lower():
          return False
        start+=1
        end-=1


      return True



# Soluction 2
class Solution2:
    def isPalindrome(self, s: str) -> bool:
        s = "".join([i for i in s if i.isalnum()]).lower()
        return s == s[::-1]